﻿using AutoMapper;
using Dm.util;
using MediatR;
using ProductIntory.Domains;
using ProductIntory.ErrorCodes;
using ProductIntory.Infrastructures.Interface;
using ProductIntory.Read.Apis.Application.Command;
using SqlSugar;
using System.DirectoryServices.Protocols;

namespace ProductIntory.Read.Apis.Application.CommandHandler
{
    public class LoginCommandHandler : IRequestHandler<LoginCommand, ApiResult<int>>
    {
        IBaseRepository<Useres> Useres;

        public LoginCommandHandler(IBaseRepository<Useres> Useres)
        {
            this.Useres = Useres;
        }

        public async Task<ApiResult<int>> Handle(LoginCommand request, CancellationToken cancellationToken)
        {
            var cout = Useres.GetAll().Where(x => x.User_Name == request.User_Name && x.User_PassWord == request.User_PassWord).Count();
            if (cout > 0)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.success,
                    Message = "登录成功",
                    Data = 1
                };
            }
            else 
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.error,
                    Message = "用户名或密码错误",
                    Data = 0
                };
            }
        }
    }
}
